using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class TypeDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public TypeDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Type table.
		/// </summary>
		public void Insert(TypeDTO type)
		{
			ValidationUtility.ValidateArgument("type", type);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TypeName", type.TypeName)
			};

			type.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "sp_TypeInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the Type table.
		/// </summary>
		public void Update(TypeDTO type)
		{
			ValidationUtility.ValidateArgument("type", type);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", type.Id),
				new SqlParameter("@TypeName", type.TypeName)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_TypeUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the sp_Type table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_TypeDelete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Type table.
		/// </summary>
		public TypeDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_TypeSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the Type table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_TypeSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the Type table.
		/// </summary>
		public List<TypeDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_TypeSelectAll"))
			{
				List<TypeDTO> typeDTOList = new List<TypeDTO>();
				while (dataReader.Read())
				{
					TypeDTO typeDTO = MapDataReader(dataReader);
					typeDTOList.Add(typeDTO);
				}

				return typeDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Type table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_TypeSelectAll");
		}

		/// <summary>
		/// Creates a new instance of the TypeDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private TypeDTO MapDataReader(SqlDataReader dataReader)
		{
			TypeDTO typeDTO = new TypeDTO();
			typeDTO.Id = dataReader.GetInt32("Id", 0);
			typeDTO.TypeName = dataReader.GetString("TypeName", null);

			return typeDTO;
		}

		#endregion
	}
}
